package com.zq.it.action.admin;

import java.util.List;

import javax.annotation.Resource;

import com.zq.it.action.BaseAction;
import com.zq.it.pojo.Course;
import com.zq.it.service.admin.CourseService;
import com.zq.it.tool.Constant;
import com.zq.it.tool.EtyLsName;
import com.zq.it.tool.FileName;
import com.zq.it.tool.FileType;
import com.zq.it.tool.HeadTitle;

import net.sf.json.JSONArray;

public class CourseAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	@Resource
	private CourseService courseService;
	private Course course;	
	private int [] courseGroupId;
	
	public void searchCourseList(){
		List list=courseService.getAllCourse();
		uploadList(EtyLsName.COURSE_LS.name(),list);
		print(JSONArray.fromObject(list,Constant.getJcf(new String[]{"chapters"},"YYYY-MM-dd")).toString(),"UTF-8","APPLICATION/JSON");
	}
	public void searchCourseListByHQL(){
		List list=courseService.getCourseListByHQL(course);
		uploadList(EtyLsName.COURSE_LS.name(),list);
		print(JSONArray.fromObject(list,Constant.getJcf(new String[]{"chapters"},"YYYY-MM-dd")).toString(),"UTF-8","APPLICATION/JSON");
	}
	
	public void deleteCourseGroupId(){
		 courseService.removeAllCourse(courseGroupId);
		 print("已删除"+courseGroupId.length+"条记录！","UTF-8","text/html");
	}
	public void addCoursePo(){
		courseService.addCourse(course);
		print("<script>parent.closeWindow('已添加');</script>","UTF-8","text/html");	
	}
	public void modifyCoursePo(){
		courseService.renewalCourse(course);
		print("<script>parent.closeWindow('已修改！');</script>","UTF-8","text/html");	
	}
	public void importCourseList(){	
		String msgStr="";
		List<Course> list=courseService.importCourseList(getUpload());
		//System.out.println("size"+list.size());
		if(list.size()>0){
		 getRequest().getSession().setAttribute("existCourseList",list);	
		 msgStr="existList";
		}else{
			msgStr="已导入成功!";
		}
		print("<script>parent.importBackResult('"+msgStr+"','webpage/adminpage/course/existCourse.jsp');</script>","UTF-8","TEXT/HTML");
	}
	public void exportCourseList(){				
		download(courseService.exportCourseList(HeadTitle.CN_COURSEHEAD.getArray(),(List<Course>)getHttpSession().getAttribute(EtyLsName.COURSE_LS.name())),FileName.COURSE_LIST_NAME.toString()+FileType.XLSX);
	}
	public void addCourse(){
		courseService.addCourse(course);		
	}
	
	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course){
		this.course = course;
	}
	public int[] getCourseGroupId() {
		return courseGroupId;
	}
	public void setCourseGroupId(int[] courseGroupId) {
		this.courseGroupId = courseGroupId;
	}
	public void deleteCourse(){
		courseService.removeCourse(course);
	}
	
	
}
